﻿using QLBH.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLBH.DATA
{
    public class SanPhamData
    {
        private static QLBHEntities db = new QLBHEntities();
        public string Error { set; get; }

        public List<SAN_PHAM> TimSanPhamTheoTen(string ten_san_pham, int tong_so_dong)
        {

            List<SAN_PHAM> list = new List<SAN_PHAM>();
            if (ten_san_pham == "")
            {
                list = db.SAN_PHAM.Include("NHOM_SAN_PHAM").Where(p => p.DA_XOA == false).ToList().Skip(tong_so_dong).Take(20).ToList().Select(p => new SAN_PHAM()
                    {
                        CO_HAN_SU_DUNG = p.CO_HAN_SU_DUNG,
                        DA_XOA = p.DA_XOA,
                        DAI = p.DAI,
                        DAY = p.DAY,
                        DON_VI_TINH = p.DON_VI_TINH,
                        GIA_BAN = p.GIA_BAN,
                        GIAM_GIA = p.GIAM_GIA,
                        HINH = p.HINH,
                        LAN_SUA_CUOI = p.LAN_SUA_CUOI,
                        MA_NHOM_SAN_PHAM = p.MA_NHOM_SAN_PHAM,
                        MA_SAN_PHAM = p.MA_SAN_PHAM,
                        MAU_SAC = p.MAU_SAC,
                        MO_TA = p.MO_TA,
                        NGUOI_SUA = p.NGUOI_SUA,
                        NHA_SAN_XUAT = p.NHA_SAN_XUAT,
                        NHOM_SAN_PHAM = new NHOM_SAN_PHAM()
                        {
                            MA_NHOM_SAN_PHAM = p.NHOM_SAN_PHAM.MA_NHOM_SAN_PHAM,
                            TEN_NHOM_SAN_PHAM = p.NHOM_SAN_PHAM.TEN_NHOM_SAN_PHAM
                        },
                        RONG = p.RONG,
                        SERIAL = p.SERIAL,
                        TEN_SAN_PHAM = p.TEN_SAN_PHAM,
                        TON_TOI_DA = p.TON_TOI_DA,
                        TON_TOI_THIEU = p.TON_TOI_THIEU,
                        XUAT_XU = p.XUAT_XU
                    }).ToList();
            }
            else
                list = db.SAN_PHAM.Include("NHOM_SAN_PHAM").Where(p => p.DA_XOA == false && p.TEN_SAN_PHAM.Contains(ten_san_pham)).ToList().Skip(tong_so_dong).Take(20).ToList().Select(p => new SAN_PHAM()
                    {
                        CO_HAN_SU_DUNG = p.CO_HAN_SU_DUNG,
                        DA_XOA = p.DA_XOA,
                        DAI = p.DAI,
                        DAY = p.DAY,
                        DON_VI_TINH = p.DON_VI_TINH,
                        GIA_BAN = p.GIA_BAN,
                        GIAM_GIA = p.GIAM_GIA,
                        HINH = p.HINH,
                        LAN_SUA_CUOI = p.LAN_SUA_CUOI,
                        MA_NHOM_SAN_PHAM = p.MA_NHOM_SAN_PHAM,
                        MA_SAN_PHAM = p.MA_SAN_PHAM,
                        MAU_SAC = p.MAU_SAC,
                        MO_TA = p.MO_TA,
                        NGUOI_SUA = p.NGUOI_SUA,
                        NHA_SAN_XUAT = p.NHA_SAN_XUAT,
                        NHOM_SAN_PHAM = new NHOM_SAN_PHAM()
                        {
                            MA_NHOM_SAN_PHAM = p.NHOM_SAN_PHAM.MA_NHOM_SAN_PHAM,
                            TEN_NHOM_SAN_PHAM = p.NHOM_SAN_PHAM.TEN_NHOM_SAN_PHAM
                        },
                        RONG = p.RONG,
                        SERIAL = p.SERIAL,
                        TEN_SAN_PHAM = p.TEN_SAN_PHAM,
                        TON_TOI_DA = p.TON_TOI_DA,
                        TON_TOI_THIEU = p.TON_TOI_THIEU,
                        XUAT_XU = p.XUAT_XU
                    }).ToList();
            return list;
        }


        private string TaoMa()
        {
            string result = "";
            if (db.SAN_PHAM.ToList().Count <= 0)
            {
                result = "0001";
            }
            else
            {
                int maxId = 0;

                foreach (var item in db.SAN_PHAM.ToList())
                {
                    if (maxId < int.Parse(item.MA_SAN_PHAM.Substring(2)))
                    {
                        maxId = int.Parse(item.MA_SAN_PHAM.Substring(2));
                    }
                }
                switch ((maxId+1).ToString().Length)
                {
                    case 1:
                        result = "000" + (maxId + 1);
                        break;


                    case 2:
                        result = "00" + (maxId + 1);
                        break;


                    case 3:
                        result = "0" + (maxId + 1);
                        break;


                    case 4:
                        result = "" + (maxId + 1);
                        break;
                    default:
                        break;
                }
            }
            return "SP" + result;
        }

        public bool ThemMoi(SANPHAMPARAM SanPham)
        {
            try
            {
                SAN_PHAM pb = new SAN_PHAM();
                pb.MA_SAN_PHAM = SanPham.MA_SAN_PHAM;
                pb.TEN_SAN_PHAM = SanPham.TEN_SAN_PHAM;
                pb.MA_NHOM_SAN_PHAM = SanPham.MA_NHOM_SAN_PHAM;
                pb.GIA_BAN = SanPham.GIA_BAN;
                pb.DON_VI_TINH = SanPham.DON_VI_TINH;
                pb.GIAM_GIA = SanPham.GIAM_GIA;
                pb.MO_TA = SanPham.MO_TA;
                pb.NHA_SAN_XUAT = SanPham.NHA_SAN_XUAT;
                pb.XUAT_XU = SanPham.XUAT_XU;
                pb.MAU_SAC = SanPham.MAU_SAC;
                pb.CO_HAN_SU_DUNG = SanPham.CO_HAN_SU_DUNG;
                pb.TON_TOI_THIEU = SanPham.TON_TOI_THIEU;
                pb.TON_TOI_DA = SanPham.TON_TOI_DA;
                pb.DAI = SanPham.DAI;
                pb.RONG = SanPham.RONG;
                pb.DAY = SanPham.DAY;
                pb.HINH = SanPham.HINH;
                pb.SERIAL = SanPham.SERIAL;
                pb.DA_XOA = false;
                pb.LAN_SUA_CUOI = DateTime.Now;
                pb.NGUOI_SUA = SanPham.NGUOI_SUA;

                if (pb.MA_SAN_PHAM.Trim().Length <= 0)
                {
                    pb.MA_SAN_PHAM = TaoMa();
                }
                else if (db.SAN_PHAM.Any(p => p.MA_SAN_PHAM == pb.MA_SAN_PHAM))
                {
                    Error = "Mã sản phẩm đã tồn tại";
                    return false;
                }

                db.SAN_PHAM.Add(pb);
                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message;
                return false;
            }
        }

        public bool CapNhat(SANPHAMPARAM SanPham)
        {
            try
            {

                SAN_PHAM pb = db.SAN_PHAM.Where(p => p.MA_SAN_PHAM == SanPham.MA_SAN_PHAM).FirstOrDefault();
                if (pb == null)
                {
                    Error = "sản phẩm không tồn tại";
                    return false;
                }

                if (pb.LAN_SUA_CUOI.ToString("ddMMyyyyhhmmss") != SanPham.LAN_SUA_CUOI.ToString("ddMMyyyyhhmmss"))
                {
                    Error = "sản phẩm đã được sửa bởi " + pb.NGUOI_SUA + " Vào lúc : " + pb.LAN_SUA_CUOI.ToString("dd/MM/yyyy hh:mm:ss");
                    return false;
                }

                pb.TEN_SAN_PHAM = SanPham.TEN_SAN_PHAM;
                pb.MA_NHOM_SAN_PHAM = SanPham.MA_NHOM_SAN_PHAM;
                pb.GIA_BAN = SanPham.GIA_BAN;
                pb.DON_VI_TINH = SanPham.DON_VI_TINH;
                pb.GIAM_GIA = SanPham.GIAM_GIA;
                pb.MO_TA = SanPham.MO_TA;
                pb.NHA_SAN_XUAT = SanPham.NHA_SAN_XUAT;
                pb.XUAT_XU = SanPham.XUAT_XU;
                pb.MAU_SAC = SanPham.MAU_SAC;
                pb.CO_HAN_SU_DUNG = SanPham.CO_HAN_SU_DUNG;
                pb.TON_TOI_THIEU = SanPham.TON_TOI_THIEU;
                pb.TON_TOI_DA = SanPham.TON_TOI_DA;
                pb.DAI = SanPham.DAI;
                pb.RONG = SanPham.RONG;
                pb.DAY = SanPham.DAY;
                pb.HINH = SanPham.HINH;
                pb.SERIAL = SanPham.SERIAL;
                pb.DA_XOA = false;
                pb.LAN_SUA_CUOI = DateTime.Now;
                pb.NGUOI_SUA = SanPham.NGUOI_SUA;

                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message + "--" + ex.InnerException.Message;
                return false;
            }
        }

        public bool Xoa(string[] MA_SAN_PHAM)
        {
            try
            {
                for (int i = 0; i < MA_SAN_PHAM.Length; i++)
                {
                    string m = MA_SAN_PHAM[i];
                    SAN_PHAM n = db.SAN_PHAM.Where(p => p.MA_SAN_PHAM == m).FirstOrDefault();
                    if (n == null)
                    {
                        Error = "sản phẩm không tồn tại";
                        return false;
                    }

                    n.DA_XOA = true;
                    db.SaveChanges();
                }

                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message + "--" + ex.InnerException.Message;
                return false;
            }
        }
    }
}